package com.appliance.chamliga.download;

import android.content.Context;
import android.os.Handler;

import com.appliance.chamliga.RootActivity;

public class DownloadRssThread extends DownloadThread {

	// variables from RootActivity
	protected final static String PATH_DIR = RootActivity.PATH_DIR;
	protected final static String rssFileName = RootActivity.rssUciNewsFilename;

	protected Context context;
	protected Handler mHandler = new Handler();

	int nr = -1;
	String extension = "xml";

	public DownloadRssThread(Context con) {
		context = con;
	}
	
	public DownloadRssThread(Context con, int input) {
		if (input < RootActivity.getDownloadLength())
			nr = input;
	}

	public DownloadRssThread(Context con, String input) {
		context = con;
		nr = RootActivity.getRssNumber(input);
	}
	
	@Override
	public void run() {
		try {
			if (nr == -1)
				for (int i = 0; i < RootActivity.getDownloadLength(); i++) {
					if (!DownloadFromUrl(RootActivity.getRss(i),
							PATH_DIR, rssFileName + i, extension, i)) {
						mHandler.post(new Runnable() {

							public void run() {
								setNewToast("No Internet");
							}
						});
					}
				}
			else {
				if (!DownloadFromUrl(RootActivity.getRss(nr),
						PATH_DIR, rssFileName + nr,
						extension, nr)) {
					
					mHandler.post(new Runnable() {

						public void run() {
							setNewToast("No Internet");
						}
					});
				}
			}
		} catch (Exception e) {
			// Log.e("## THREAD", "fail: " + e.toString());
		}
	}
	
}
